cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        _chatRoot:null,
        _iptChat:null,
        
        _quickChatInfo:null,
        _btnChat:null,

        _chatCont:null,
        _chatItem:null
    },

    // use this for initialization
    onLoad: function () {
        if(cc.vv == null){
            return
        };
        
        cc.vv.chat = this;
        
        this._btnChat = this.node.getChildByName("btn_chat");
        cc.vv.utils.addClickEvent(this._btnChat,this.node,"Chat","onBtnChatClicked");
        this._btnChat.active = cc.vv.replayMgr.isReplay() == false;
        
        this._chatRoot = this.node.getChildByName("chat");
        this._chatRoot.active = false;
        // 聊天按钮
        var tabChat = this._chatRoot.getChildByName("tabChat");
        cc.vv.utils.addClickEvent(tabChat,this.node,"Chat","onTabClicked");
        // 常用界面
        // 每条常用语点击
        var _quickTabCont=cc.find("quickchatlist/view/content",this._chatRoot);
        for (var i = 0; i < _quickTabCont.childrenCount; i++) {
            var item=_quickTabCont.children[i];
            cc.vv.utils.addClickEvent(item,this.node,"Chat","onQuickChatItemClicked")
        };
        // 表情界面
        // 每个表情点击
        var _emojiTabCont=cc.find("emojis/view/content",this._chatRoot);
        for (var i = 0; i < _emojiTabCont.childrenCount; i++) {
            var item=_emojiTabCont.children[i];
            cc.vv.utils.addClickEvent(item,this.node,"Chat","onEmojiItemClicked")
        };
        // 输入框
        this._iptChat = this._chatRoot.getChildByName("iptChat").getComponent(cc.EditBox);
        // 点击游戏桌面隐藏
        cc.vv.utils.addClickEvent(this.node,this.node,"Chat","onBgClicked");
        // 发送按钮
        var sendBt = this._chatRoot.getChildByName("btnSend");
        cc.vv.utils.addClickEvent(sendBt,this.node,"Chat","onBtnSendChatClicked");
        this._quickChatInfo = {};
        this._quickChatInfo["item0"] = {index:0,content:"快点啊，都等到我花儿都谢谢了！",sound:"fix_msg_1.mp3"};
        this._quickChatInfo["item1"] = {index:1,content:"怎么又断线了，网络怎么这么差啊！",sound:"fix_msg_2.mp3"};
        this._quickChatInfo["item2"] = {index:2,content:"不要走，决战到天亮！",sound:"fix_msg_3.mp3"};
        this._quickChatInfo["item3"] = {index:3,content:"你的牌打得也太好了！",sound:"fix_msg_4.mp3"};
        this._quickChatInfo["item4"] = {index:4,content:"你是妹妹还是哥哥啊？",sound:"fix_msg_5.mp3"};
        this._quickChatInfo["item5"] = {index:5,content:"和你合作真是太愉快了！",sound:"fix_msg_6.mp3"};
        this._quickChatInfo["item6"] = {index:6,content:"大家好，很高兴见到各位！",sound:"fix_msg_7.mp3"};
        this._quickChatInfo["item7"] = {index:7,content:"各位，真是不好意思，我得离开一会儿。",sound:"fix_msg_8.mp3"};
        this._quickChatInfo["item8"] = {index:8,content:"不要吵了，专心玩游戏吧！",sound:"fix_msg_9.mp3"}
        // 主动聊天记录保留
        this._chatCont = cc.find("chat/view/content",this._chatRoot);
        this._chatItem = this._chatCont.getChildByName("item");
        this._chatItem.removeFromParent();
        var self=this;
        this.node.on('chat_push',function(data){
            self.addChatItem(data.detail)
        });
    },
    addChatItem:function(data){
        var node=this.getViewItem();
        var nameLabel=node.getChildByName("name").getComponent(cc.Label);
        nameLabel.string=cc.vv.gameNetMgr.getNameByID(data.sender)+":";
        var chatLabel=node.getChildByName("chat").getComponent(cc.Label);
        chatLabel.string=data.content
    },
    getViewItem:function(index){
        var node = cc.instantiate(this._chatItem);
        this._chatCont.addChild(node);
        return node
    },
    getQuickChatInfo(index){
        var key = "item" + index;
        return this._quickChatInfo[key] 
    },
    
    onBtnChatClicked:function(){
        this._chatRoot.active = true
    },
    
    onBgClicked:function(){
        this._chatRoot.active = false
    },
    
    onQuickChatItemClicked:function(event){
        this._chatRoot.active = false;
        var info = this._quickChatInfo[event.target.name];
        cc.vv.net.send("quick_chat",info.index)
    },
    
    onEmojiItemClicked:function(event){
        // console.log(event.target.name);
        this._chatRoot.active = false;
        cc.vv.net.send("emoji",event.target.name)
    },
    
    onBtnSendChatClicked:function(){
        this._chatRoot.active = false;
        if(this._iptChat.string == ""){
            return
        };
        cc.vv.net.send("chat",this._iptChat.string);
        this._iptChat.string = ""
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
